<template>
  <div class="detail-container">
    <article class="article-content">
      <div class="product-info">
        <div class="content-body description-content" v-html="product.description"></div>
      </div>
    </article>
  </div>
</template>

<script>
import productApi from "@/api/product";
import categoryApi from "@/api/category";

export default {
  name: 'ProductDetail',
  data() {
    return {
      product: {}, // 商品详情数据
      categoryName: '',
    };
  },
  created() {
    this.loadProductDetail();
    this.load();
  },
  methods: {
    async load() {
      try {
        const productRes = await productApi.getById(1);
        this.product = productRes.data;
        this.adjustDescriptionImages();
      } catch (error) {
        console.error("加载数据失败:", error);
      }
    },
    
    loadProductDetail() {
      productApi.getById(1).then((res) => {
        this.product = res.data;
        this.adjustDescriptionImages();
      });
    },

    adjustDescriptionImages() {
      this.product.description = this.product.description?.replace(
        /<img([^>]*)>/gi,
        (match, attributes) => {
          const cleanedAttributes = attributes.replace(/style="[^"]*"/i, "");
          return `<img${cleanedAttributes} style="max-width: 100%; height: auto; object-fit: contain; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); margin: 1rem 0;">`;
        }
      );
    },

    goBack() {
      try {
        this.$router.go(-1);
      } catch (error) {
        this.$router.push("/");
      }
    }
  }
};
</script>

<style scoped>
.detail-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 20px 0;
  background-color: #c1f1c9; /* Light gray background */
  border-radius: 12px; /* 添加圆角 */
  margin-top: 20px;
  margin-bottom: 40px;
  margin-left: 12px;
  margin-right: 12px;
}

.article-content {
  max-width: 800px;
  margin: 0 auto;
}

.product-info {
  font-size: 1.05rem;
}

.description-content {
  word-break: break-word;
}

.description-content >>> p {
  margin-bottom: 1.5rem;
  color: #444;
}

.description-content >>> h2,
.description-content >>> h3 {
  margin-top: 2rem;
  margin-bottom: 1rem;
  color: #222;
}

.description-content >>> a {
  color: #667eea;
  text-decoration: none;
  transition: color 0.2s;
}

.description-content >>> a:hover {
  color: #764ba2;
  text-decoration: underline;
}

@media screen and (max-width: 768px) {
  .detail-container {
    padding: 10px 0;
  }
}

@media screen and (max-width: 480px) {
  .detail-container {
    padding: 5px 0;
  }
}
</style>